
#!/bin/bash

set -e
current_path=$(cd "$(dirname "$0")"; pwd) 
tensorrt_path=/usr/local/src
PROFILE="/etc/profile.d"
CUDASH=$PROFILE/cuda.sh
LDCFG="/etc/ld.so.conf.d/cuda.conf"
TRTSH=$PROFILE/trt.sh
TRTCFG="/etc/ld.so.conf.d/trt.conf"
chmod +x *.run
expect <<-EOF
set timeout -1
spawn ./cuda_10.0.130_410.48_linux.run
send "q"
expect "*accept*" { send "accept\r";}
expect "*continue*" { send "y\r";}
expect "*Driver*" {send "n\r";}
expect "*Toolkit*" {send "y\r";}
expect "*Location*" {send "\r";}
expect "*symbolic*" {send "y\r";}
expect "*Samples*" {send "n\r";}
expect eof
EOF
expect <<-EOF
set timeout -1
spawn ./cuda_10.0.130.1_linux.run
send "q"
expect "*accept*" { send "accept\r";}
expect "*directory*" { send "\r";}
expect eof
EOF

echo abc>$CUDASH
sed -i 's/abc/export PATH=\/usr\/local\/cuda\/bin${PATH:+:$PATH}}/' $CUDASH
echo abc>$LDCFG
sed -i 's/abc/\/usr\/local\/cuda\/lib64/' $LDCFG

tar zxf cudnn-??.?-*.tgz
soname=$(basename cuda/lib64/libcudnn.so.?)
cp cuda/include/cudnn.h /usr/local/cuda/include
cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
rm /usr/local/cuda/lib64/${soname}
ln -sf /usr/local/cuda/lib64/libcudnn.so.?.?.?  /usr/local/cuda/lib64/$soname
rm cuda -rf

cd $tensorrt_path
tar zxf $current_path/TensorRT*.gz
trtname=$(basename ${tensorrt_path}/TensorRT-*)
ln -s ${tensorrt_path}/${trtname} tensorrt
echo abc>$TRTSH
sed -i 's/abc/export PATH=\/usr\/local\/src\/tensorrt\/bin${PATH:+:$PATH}}/' $TRTSH
echo abc>$TRTCFG
sed -i 's/abc/\/usr\/local\/src\/tensorrt\/lib/' $TRTCFG


ldconfig